package com.tz.farmer.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.smartPark.msframe.result.ResultEx;
import com.tz.farmer.enums.ErrorCodeEnum;
import com.tz.farmer.service.WebFarmerActivesService;
import com.tz.farmer.so.FarmerActivesSo;

import com.tz.farmer.util.POIStyleImpl;
import com.tz.farmer.util.ResultExM;
import com.tz.farmer.vo.EnrollInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.util.List;


/**
 * 食安活动
 */
@Api(tags = "食安活动-web")
@Slf4j
@RestController
@RequestMapping("/actives")
public class WebFarmerActivesController {

    @Resource
    WebFarmerActivesService webFarmerActivesService;


    /**
     * 添加查询列表
     * @param so
     * @return
     */
    @GetMapping("/listConditions")
    @ApiOperation("用户条件查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "flag", value = "0:农 1：渔", dataType = "int", defaultValue = "", required = true),
            @ApiImplicitParam(name = "keyword", value = "关键字", dataType = "string", defaultValue = "", required = false),
            @ApiImplicitParam(name = "releaseTime", value = "时间", dataType = "string", defaultValue = "", required = false),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", defaultValue = "", required = true),
            @ApiImplicitParam(name = "currentPage", value = "每页条数", dataType = "int", defaultValue = "", required = true)
    })
    public ResultEx getActivesListByConditions(FarmerActivesSo so){
        return webFarmerActivesService.getActivesListByConditions(so);
    }

    /**
     * 添加修改软删除
     * @param so
     * @return
     */
    @PostMapping("/editAdd")
    @ApiOperation("添加修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "flag", value = "0：农  1：鱼", dataType = "int", defaultValue = "", required = true),
            @ApiImplicitParam(name = "doFlag", value = "1：添加  2：修改", dataType = "string", defaultValue = "", required = true),
            @ApiImplicitParam(name = "id", value = "自增id", dataType = "int", defaultValue = "", required = true),
            @ApiImplicitParam(name = "title", value = "标题", dataType = "string", defaultValue = "", required = true),
            @ApiImplicitParam(name = "activeText", value = "活动内容", dataType = "string", defaultValue = "", required = true),
            @ApiImplicitParam(name = "joinPeople", value = "参与人群", dataType = "string", defaultValue = "", required = true),
            @ApiImplicitParam(name = "phone", value = "咨询电话", dataType = "string", defaultValue = "", required = true),
            @ApiImplicitParam(name = "address", value = "活动地点", dataType = "string", defaultValue = "", required = true),
            @ApiImplicitParam(name = "startTime", value = "开始时间", dataType = "string", defaultValue = "", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "string", defaultValue = "", required = true),
            @ApiImplicitParam(name = "isTop", value = "置顶", dataType = "string", defaultValue = "", required = true),
            @ApiImplicitParam(name = "activeEndTime", value = "报名截止时间", dataType = "string", defaultValue = "", required = true),
            @ApiImplicitParam(name = "isSued", value = "是否发布 0：否 1 ：是 默认（1）", dataType = "int", defaultValue = "", required = true)
    })
    public ResultEx  updateAndAddActives(@RequestBody FarmerActivesSo so){
        return webFarmerActivesService.updateAndAddActives(so);
    }

    /**
     * 查看报名人信息
     */
    @GetMapping("/applyPeople")
    @ApiOperation("条件查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "活动id", dataType = "int", defaultValue = "", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", defaultValue = "", required = true),
            @ApiImplicitParam(name = "currentPage", value = "当前页", dataType = "int", defaultValue = "", required = true)
    })
    public ResultEx getActivesApplyPeopleList(FarmerActivesSo so){
        return webFarmerActivesService.getActivesApplyPeopleList(so);
    }

    /**
     * 查看单条记录
     */
    @GetMapping("/selectOne")
    @ApiOperation("查看一条记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "活动id", dataType = "int", defaultValue = "", required = true)
    })
    public ResultEx getOneDetails(FarmerActivesSo so){
        return webFarmerActivesService.getOneDetails(so);
    }


    /**
     * 导出
     * @param so
     * @return
     */
    @GetMapping("/applyPeopleDownLoad")
    @ApiOperation("导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "活动id", dataType = "int", defaultValue = "", required = true)
    })
    public ResultEx getCodeByProductId(FarmerActivesSo so,HttpServletResponse resp) {
        try {
            List<EnrollInfoVo> data = webFarmerActivesService.getApplyPeopleDownLoadList(so);
            if (data.size() <= 0) {
                return ResultExM.builder().makeResultNoData();
            }
            ExportParams exportParams = new ExportParams();
            exportParams.setStyle(POIStyleImpl.class);
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, EnrollInfoVo.class, data);
            // 告诉浏览器用什么软件可以打开此文件
            resp.setHeader("content-Type", "application/vnd.ms-excel");
            // 下载文件的默认名称
            resp.setHeader("Content-Disposition", "attachment;filename=" + new String("活动人员表".getBytes(StandardCharsets.UTF_8), "iso8859-1") + ".xls");
            //编码
            resp.setCharacterEncoding("UTF-8");
            workbook.write(resp.getOutputStream());
            return null;
        } catch (Exception ex) {
            log.error("导出失败：", ex);
            return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_ERROR_EXPORT);
        }
    }


    /**
     * 硬删除单条记录
     */
    @GetMapping("/deleteOne")
    @ApiOperation("硬删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "int", defaultValue = "", required = true)
    })
    public ResultEx deleteOne(FarmerActivesSo so){
        return webFarmerActivesService.deleteOne(so);
    }


}
